<?php
App::uses('AppController', 'Controller');
/**
 * Stores Controller
 *
 * @property Store $Store
 */
class StoresController extends AppController {
	public function beforeFilter() {
		parent::beforeFilter ();
		$this->Auth->allow ( '*' );
	}
	public function queryXml() {
		$this->set ( 'stores', $this->paginate () );
		$this->set ( compact ( 'stores' ) );
		$this->set ( '_serialize', array (
				'stores'
		) );
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Store->recursive = 0;
		$this->set('stores', $this->paginate());
	}

/**
* storeList method
* @param int $tid , $pid
* @return void
*/
	public function storeList($tid = null,$sid = null){
		$this->layout="ajax";
		if($tid<5 || $tid == null){
			if($tid==null)$tid=1;
		    $stores = $this->Store->query("select * from categories as A , store_categories as B , stores as C where A.id = B.category_id and B.store_id = C.id and A.id = '$tid'");
		}else{
			$stores = $this->Store->query("select * from stores as C");
		}
		if($sid != null){
			$promos = $this->Store->query("select * from promos where store_id = '$sid' and begin_date <= NOW() and end_date >= NOW()");
			//this->Html->link();
			//$this->redirect("/mcouponMgr/Stores/storeList/$flag/$pid#page5");
			//$this->redirect(array("controller" => "Stores", 'action' => 'storeList',$flag , $pid."#page5"));
		}
		$this->set(compact('tid','sid','stores',"promos"));
	}
/**
* storeShow method
*
* @param string $id
* 
*/
	public function storeShow($tid = null, $sid = null , $pid = null ){
		$this->layout="ajax";
		$promodetails = $this->Store->query("select * from promos as A , stores as B where A.id = '$pid' and A.store_id = '$sid' and B.id = '$sid'");
		$this->set(compact('tid','promodetails'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Store->id = $id;
		if (!$this->Store->exists()) {
			throw new NotFoundException(__('Invalid store'));
		}
		$this->set('store', $this->Store->read(null, $id));
	}
/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Store->create();
			if ($this->Store->save($this->request->data)) {
				$this->Session->setFlash(__('The store has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The store could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Store->id = $id;
		if (!$this->Store->exists()) {
			throw new NotFoundException(__('Invalid store'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Store->save($this->request->data)) {
				$this->Session->setFlash(__('The store has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The store could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Store->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Store->id = $id;
		if (!$this->Store->exists()) {
			throw new NotFoundException(__('Invalid store'));
		}
		if ($this->Store->delete()) {
			$this->Session->setFlash(__('Store deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Store was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function ajaxQueryStores() {
		$id=$this->params['url']['category_id'];
		$startI=$this->params['url']['startI'];
		$sgmap=$this->params['url']['sgmap'];
	    if($sgmap=="yes"){
	    	$queryStore = $this->Store->query("select * from categories as A , store_categories as B , stores as C where A.id = B.category_id and B.store_id = C.id and A.id = '$id' and C.status = '1'");
	    }elseif($id<5){
			$queryStore = $this->Store->query("select * from categories as A , store_categories as B , stores as C where A.id = B.category_id and B.store_id = C.id and A.id = '$id' and C.status = '1' limit $startI , 6");
		}else{
			$queryStore = $this->Store->query("select * from stores as C where C.status = '1' limit $startI , 6");
		}
		$this->layout = 'ajax';
		$this->Store->recursive = 0;
		//$queryStore = $this->Store->find ( 'all' );
		$this->set ( compact ( 'queryStore' ) );
	}
}
